#include "zos.h"
#include "zos_config.h"

//检测网络是否连接成功,成功返回:ZOS_EOK,失败返回:ZOS_ERROR
zos_err_t network_init(void)
{
	zos_uint8_t i=20;
	zos_err_t mode;
	zos_uint8_t cfun;
	zos_uint8_t rssi,cgact;
	struct nb_time_t time;
	nb_serving_cell_info_t rcv_servingcell_info;
	char *str = ZOS_NULL;
	str = zos_malloc(50);

	zos_printf("nb power by psm:%d\r\n",nb_get_powenon_from_deepsleep());
	zos_printf("nb power type:%d\r\n",nb_get_power_type());
	zos_printf("nb reset type:%d\r\n",nb_get_reset_type());
	nb_get_IMEI(str);
	str[15] = '\0';
	zos_printf("IMEI = %s\r\n",str);
	if(nb_get_IMSI(str) == ZOS_ERROR)
	{
		return ZOS_ERROR;
	}
	zos_printf("IMSI = %s\r\n",str);
	str[15] = '\0';
	nb_get_NCCID(str);
	zos_printf("ICCID = %s\r\n",str);
	str[20] = '\0';
	do{
		if(nb_get_csq(&rssi) == ZOS_EOK)
		{
			zos_printf("csq = %d\r\n",rssi);
			if(rssi<32)
			{
				break;
			}
		}
		zos_task_delay(1000);
	}while(i--);

	if(i>0)
	{
		do{
			if(nb_get_CGACT(&cgact) == ZOS_EOK)
			{
				zos_printf("cgact=%d\r\n",cgact);
				if(cgact == 1)
				{
					break;
				}
			}
			zos_task_delay(1000);
		}while(i--);
		if(i <= 0)
		{
			zos_printf("module connect to net failed\r\n");
			return ZOS_ERROR;
		}
		zos_task_delay(1000);
		if(zos_rtc_get_time(&time) == ZOS_EOK)
		{
			zos_printf("date:%d-%d-%d time:%d-%d-%d\r\n",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec);
		}
		nb_get_servingcell_info(&rcv_servingcell_info);
		zos_printf("cell info:%d,%d,%d,%d,%d,%d,%d\r\n",
        rcv_servingcell_info.EARFCN,
        rcv_servingcell_info.PCI,
        rcv_servingcell_info.CellID,
        rcv_servingcell_info.Signalpower,
        rcv_servingcell_info.RSRQ,
        rcv_servingcell_info.Totalpower,
        rcv_servingcell_info.SNR);
		zos_printf("Module connect to net successful\r\n");	
		return ZOS_EOK;	
	}
	zos_printf("module connect to net failed\r\n");
	return ZOS_ERROR;
}


//以下为shell命令相关函数和命令定义，如果不需要shell命令控制，可以全部删除
int demo_network(int argc, char *agrv[])
{
	return network_init();
}
#include "shell.h"

SHELL_EXPORT_CMD(demo_network,demo_network, network demo);




